package com.chenglai.goods.service;

import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.goods.dao.ProductAnswerMapper;
import com.chenglai.goods.pojo.ProductAnswer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author jiang
 * @created 2023/11/16 14:28:20
 **/
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private ProductAnswerMapper productAnswerMapper;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void addProductCommentPic(ProductAnswer productAnswer) {
        productAnswer.setAnswerTime(DateFormatHelper.currentTimes());
        productAnswer.setAnswerStatus(0);
        productAnswerMapper.insertSelective(productAnswer);

//        if (productAnswer.getParentId() != 0){
//            //回答问题数量+1
//            ProductAnswer productAnswer1 = productAnswerMapper.selectByPrimaryKey(productAnswer.getProductId());
//            productAnswer1.setAnswerNum(productAnswer1.getAnswerNum() +1 );
//            productAnswerMapper.updateByPrimaryKeySelective(productAnswer1);
//        }
    }

    @Override
    public List<ProductAnswer> getAnswerComment(Integer productId) {
        //问题列表
        List<ProductAnswer> productAnswers = productAnswerMapper.getAnswerComment(productId,0);

        for (ProductAnswer productAnswer : productAnswers) {
            //获取问题的回答列表
            List<ProductAnswer> productAnswerList = productAnswerMapper.getAnswerComment(productAnswer.getAnswerId(),1);
            productAnswer.setProductAnswerList(productAnswerList);
        }
        return productAnswers;
    }

    @Override
    public void delAnswerComment(Integer answerId) {
        if (answerId != null){
            Example example = new Example(ProductAnswer.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("answerId",answerId).andEqualTo("parentId",answerId);
            productAnswerMapper.deleteByExample(example);
        }
    }

    @Override
    public PageInfo getSlideshowList(Integer page, Integer size, Integer shopId) {
        PageHelper.startPage(page,size);
        List<ProductAnswer> answerComment = productAnswerMapper.getAnswerComment(shopId, 2);
        return new PageInfo(answerComment);
    }

    @Override
    public void updateAnswerComment(Integer answerId, Integer answerStatus) {
        ProductAnswer productAnswer = new ProductAnswer();
        productAnswer.setAnswerId(answerId);
        productAnswer.setAnswerStatus(answerStatus);
        productAnswerMapper.updateByPrimaryKeySelective(productAnswer);
    }
}
